﻿/*
This stored procedure computes the fastest execution time of the activity whose properties are given.
The execution time is given in milliseconds as an output parameter.
*/
CREATE PROCEDURE [Admin].[sp_z4s_fastest_activity_execution_time]
      @param_TestSessionId UNIQUEIDENTIFIER
    , @param_TestScenarioId UNIQUEIDENTIFIER
    , @param_ActivityId UNIQUEIDENTIFIER
    , @out_fastestExecutionTime INT OUTPUT
AS

SELECT
    @out_fastestExecutionTime = MIN(DATEDIFF(ms, Scenario.activityHistory.activityHistory_startDateTime, Scenario.activityHistory.activityHistory_endDateTime))
FROM
    Scenario.activityHistory
    INNER JOIN Scenario.activity ON Scenario.activity.activity_id = Scenario.activityHistory.activityHistory_activityId
    INNER JOIN Admin.clientSession ON Admin.clientSession.clientSession_id = Scenario.activityHistory.activityHistory_clientSessionId
    INNER JOIN Scenario.testScenario ON Admin.clientSession.clientSession_testScenarioId = Scenario.testScenario.testScenario_id
    INNER JOIN Admin.testSession ON Admin.testSession.testSession_id = Admin.clientSession.clientSession_testSessionId
WHERE
    Admin.testSession.testSession_id = @param_TestSessionId
        AND
    Scenario.testScenario.testScenario_id = @param_TestScenarioId
        AND
    Scenario.activity.activity_id = @param_ActivityId

RETURN 0